package com.to8to.scene.data;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.thrift.TBase;
import org.apache.thrift.TFieldIdEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.to8to.commons.utils.Progress;
import com.to8to.commons.utils.StringUtil;
import com.to8to.commons.utils.ThriftUtil;
import com.to8to.scene.server.Commons;
import com.to8to.scene.server.Tables;
import com.to8to.thrift.diary.consts.constConstants;
import com.to8to.thrift.diary.module.TDiary;
import com.to8to.thrift.diary.module.TDiaryStat;
import com.to8to.thrift.diary.module.TProduct;
import com.to8to.thrift.diary.module.TScene;
import com.to8to.thrift.diary.module.TSceneStat;
import com.to8to.thrift.diary.module.TStType;

class TException extends Exception
{
    public TException(TBase inner_obj, String message)
    {
        super(message);
        this.setTobject(inner_obj);
    }

    TBase tobject;

    public TBase getTobject()
    {
        return tobject;
    }

    public void setTobject(TBase tobject)
    {
        this.tobject = tobject;
    }
}

public class DataValidator
{

    public static Logger logger = LoggerFactory.getLogger(DataValidator.class);

    static Map<Integer, Map<Integer, String>> cate_map, buy_from_map, progress_map, hometype_map;

    public static Map<Integer, Map<Integer, String>> getStConfMap(String level_1, TFieldIdEnum field_1, String level_2, TFieldIdEnum field_2)
    {
        Map<Integer, Map<Integer, String>> map = new HashMap<Integer, Map<Integer, String>>();
        DBObject st_conf_level_1_db =
            Commons.mongo.findOne(Tables.StaticConfigValue, new BasicDBObject(TStType._Fields.TYPE_NAME.getFieldName(), level_1), null, null);
        TStType st_conf_1 = ThriftUtil.dbObject2ThriftObject(st_conf_level_1_db, TStType.class);
        for (String str_1 : st_conf_1.getId_map().keySet())
        {
            int id_1 = Integer.parseInt(str_1);
            DBObject st_conf_level_2_db =
                Commons.mongo.findOne(Tables.StaticConfigValue,
                    new BasicDBObject().append(TStType._Fields.TYPE_NAME.getFieldName(), level_2)
                        .append(TStType._Fields.PARENT_TYPE_ID.getFieldName(), str_1),
                    null,
                    null);

            Map<Integer, String> id_2_map = new HashMap<Integer, String>();
            TStType st_conf_level_2 = ThriftUtil.dbObject2ThriftObject(st_conf_level_2_db, TStType.class);
            for (String str_2 : st_conf_level_2.getId_map().keySet())
            {
                int id_2 = Integer.parseInt(str_2);
                id_2_map.put(id_2, st_conf_level_2.getId_map().get(str_2));
            }
            map.put(id_1, id_2_map);
        }
        return map;
    }

    /**
     * obj_id对应的<class_name>module_obj的method获取到的字段不该为空
     * @param module_obj
     * @param class_name
     * @param method
     * @param obj_id
     * @throws TException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws IllegalAccessException 
     */
    static void should_not_empty_field(TBase module_obj, String class_name, Method method, String obj_id) throws TException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException
    {
        String field_value;
        field_value = (String) method.invoke(module_obj);
        if (StringUtil.isEmpty(field_value))
        {
            logger.error("no {} of id:{} in {}", method.getName(), obj_id, class_name);
            throw new TException(module_obj, "empty");
        }
    }

    static void should_be_positive(TBase module_obj, String class_name, Method method, String obj_id) throws Exception
    {
        int field_value;
        field_value = (Integer) method.invoke(module_obj);
        if (field_value <= 0)
        {
            logger.error("{} of id:{} in {} should be positive but is {}", method.getName(), obj_id, class_name, field_value);
            throw new TException(module_obj, "positive");
        }
    }

    /**
     * obj_id对应的<class_name>module_obj的method获取到的字段应该在among_array范围内
     * @param module_obj
     * @param class_name
     * @param method
     * @param obj_id
     * @param among_array
     * @throws Exception 
     */
    static boolean should_be_among_field(TBase module_obj, String class_name, Method method, String obj_id, List<Integer> among_array)
            throws Exception
    {
        Object field_value;
        //        try
        //        {
        Object field_value_obj = method.invoke(module_obj);
        field_value = Integer.parseInt(field_value_obj.toString());
        if (!among_array.contains(field_value))
        {
            logger.error("{} of id:{} in {} should be in {}", method.getName(), obj_id, class_name, among_array);
            throw new TException(module_obj, "not in");
        }
        //        }
        //        catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
        //        {
        //            // TODO Auto-generated catch block
        //            e.printStackTrace();
        //        }
        return true;
    }

    /**
     * 查询在table中是否有<class_name>module_obj对应method字段对应的id
     * @param module_obj
     * @param class_name
     * @param method
     * @param obj_id
     * @param table
     * @throws Exception 
     */
    static void related_should_exist(TBase module_obj, String class_name, String related_id, String field_name, String obj_id, String table)
            throws Exception
    {

        if (StringUtil.isEmpty(related_id))
        {
            logger.error("target {} should not be null of {}", field_name, class_name);
            throw new TException(module_obj, "related empty");
        }
        else
        {
            DBObject related_db = Commons.mongo.findById(table, related_id);
            if (null == related_db)
            {
                logger.error("no {} with id {} exists in {} of _id:{},which was created in {}", table, related_id, class_name, obj_id);
                throw new TException(module_obj, "related not exists");
            }
        }
    }

    static void printDetails(Object obj, String... details) throws NoSuchFieldException, SecurityException, IllegalArgumentException,
            IllegalAccessException
    {
        for (String field_name : details)
        {
            Field field = obj.getClass().getDeclaredField(field_name);
            String value = field.get(obj).toString();

            if (field.getType() == int.class)
            {
                value = (new Date(1000l * Integer.parseInt(value)).toLocaleString());
            }

            DataValidator.logger.info("{}:{}", field.getName(), value);

        }
    }

    //    /**
    //     * COMMENT DATA VALIDATE		
    //     * @throws NoSuchMethodException
    //     * @throws SecurityException
    //     */
    //    static void validateComment() throws NoSuchMethodException, SecurityException
    //    {
    //        Class<?> comment_clazz = TComment.class;
    //        String comment_class_name = TComment.class.getName();
    //
    //        Progress prog = new Progress(Commons.mongo.count(Tables.Comment));
    //        for (String comment_id : Commons.mongo.ids(Tables.Comment))
    //        {
    //            DBObject comment_db = Commons.mongo.findById(Tables.Comment, comment_id);
    //            TComment comment = ThriftUtil.dbObject2ThriftObject(comment_db, TComment.class);
    //            prog.increase(logger);
    //            //FORMAT VALIDATE
    //            Method[] comment_methods = comment_clazz.getMethods();
    //            for (Method comment_method : comment_methods)
    //            {
    //                String method_name = comment_method.getName().toLowerCase();
    //                if (method_name.equals("get" + TComment._Fields.SCENE_ID.getFieldName())
    //                    || method_name.equals("get" + TComment._Fields.CONTENT.getFieldName())
    //                    || method_name.equals("get" + TComment._Fields.USER_ID.getFieldName()))
    //                    should_not_empty_field(comment, comment_class_name, comment_method, comment_id);
    //
    //                else if (method_name.equals("get" + TComment._Fields.TGT_TYPE.getFieldName()))
    //                    should_be_among_field(comment,
    //                        comment_class_name,
    //                        comment_method,
    //                        comment_id,
    //                        Arrays.asList(new Integer[] {
    //                            constConstants.TARGET_TYPE_SCENE,
    //                            constConstants.TARGET_TYPE_DIARY,
    //                            constConstants.TARGET_TYPE_PRODUCT,
    //                            constConstants.TARGET_TYPE_COMMENT }));
    //
    //                else if (method_name.equals("get" + TComment._Fields.STATUS.getFieldName()))
    //                    should_be_among_field(comment,
    //                        comment_class_name,
    //                        comment_method,
    //                        comment_id,
    //                        Arrays.asList(new Integer[] {
    //                            constConstants.ENTITY_STATUS_NORM,
    //                            constConstants.ENTITY_STATUS_DEL,
    //                            constConstants.ENTITY_STATUS_REJECT }));
    //
    //                else if (method_name.equals("get" + TComment._Fields.PLATFORM.getFieldName()))
    //                    should_be_among_field(comment,
    //                        comment_class_name,
    //                        comment_method,
    //                        comment_id,
    //                        Arrays.asList(new Integer[] {
    //                            constConstants.PLATFORM_ANDROID,
    //                            constConstants.PLATFORM_HTML5,
    //                            constConstants.PLATFORM_IOS,
    //                            constConstants.PLATFORM_PC }));
    //
    //                //				else if(method_name.equals("get"+TComment._Fields.PUBLISH_SOURCE.getFieldName()))
    //                //					should_be_among_field(comment,comment_class_name,comment_method,comment_id,
    //                //							Arrays.asList(new Integer[]{
    //                //								constConstants.PUBLISH_DIARY_SOURCE_OWNER
    //                //							}));
    //
    //                else if (method_name.equals("get" + TComment._Fields.CTIME.getFieldName()))
    //                    should_be_positive(comment, comment_class_name, comment_method, comment_id);
    //            }
    //
    //            //RELATION VALIDATE
    //            //scene
    //            related_should_exist(comment,
    //                comment_class_name,
    //                comment.getScene_id(),
    //                TComment._Fields.SCENE_ID.getFieldName(),
    //                comment_id,
    //                Tables.Scene);
    //            //target
    //            switch (comment.getTgt_type())
    //            {
    //            case constConstants.TARGET_TYPE_DIARY:
    //            {
    //                related_should_exist(comment,
    //                    comment_class_name,
    //                    comment.getDiary_id(),
    //                    TComment._Fields.DIARY_ID.getFieldName(),
    //                    comment_id,
    //                    Tables.Diary);
    //                break;
    //            }
    //            case constConstants.TARGET_TYPE_PRODUCT:
    //            {
    //                related_should_exist(comment,
    //                    comment_class_name,
    //                    comment.getProduct_id(),
    //                    TComment._Fields.PRODUCT_ID.getFieldName(),
    //                    comment_id,
    //                    Tables.Product);
    //                break;
    //            }
    //            //			case constConstants.TARGET_TYPE_COMMENT:{
    //            //				related_should_exist(comment,comment_class_name,comment.getRef_comment_id(),TComment._Fields.REF_COMMENT_ID.getFieldName(),comment_id,Tables.Comment);
    //            //				should_not_empty_field(comment,comment_class_name,comment_clazz.getMethod("getRef_comment_user_id"), comment_id);
    //            //				if(!StringUtil.isEmpty(comment.getParent_comment_id())){
    //            //					related_should_exist(comment,comment_class_name,comment.getParent_comment_id(),TComment._Fields.PARENT_COMMENT_ID.getFieldName(),comment_id,Tables.Comment);
    //            //					should_not_empty_field(comment,comment_class_name,comment_clazz.getMethod("getParent_comment_user_id"), comment_id);
    //            //				}
    //            //				break;
    //            //			}
    //            }
    //
    //            if (comment.getStatus() == constConstants.ENTITY_STATUS_REJECT)
    //            {
    //                if (StringUtil.isEmpty(comment.getRemark()))
    //                {
    //                    logger.error("no remarks when reject on comment of _id:{}", comment_id);
    //                }
    //            }
    //        }
    //    }

    /**
     * PRODUCT DATA VALIDATE		
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    static void validateProduct() throws NoSuchMethodException, SecurityException
    {
        Class<?> product_clazz = TProduct.class;
        String product_class_name = TProduct.class.getName();

        Progress prog = new Progress(Commons.mongo.count(Tables.Product));
        for (String product_id : Commons.mongo.ids(Tables.Product))
        {
            logger.info("scanning product:{}", product_id);
            DBObject product_db = Commons.mongo.findById(Tables.Product, product_id);
            TProduct product = ThriftUtil.dbObject2ThriftObject(product_db, TProduct.class);

            if ((1 != product.getStatus()) && (3 != product.getStatus()))
                continue;

            prog.increase(logger);
            //FORMAT VALIDATE
            Method[] product_methods = product_clazz.getMethods();
            for (Method product_method : product_methods)
            {
                try
                {
                    String method_name = product_method.getName().toLowerCase();
                    if (method_name.equals("get" + TProduct._Fields.SCENE_ID.getFieldName())
                        || method_name.equals("get" + TProduct._Fields.NAME.getFieldName()))
                        should_not_empty_field(product, product_class_name, product_method, product_id);

                    //              else if(method_name.equals("get"+TComment._Fields.TGT_TYPE.getFieldName()))
                    //                  should_be_among_field(product,product_class_name,product_method,product_id,
                    //                          Arrays.asList(new Integer[]{
                    //                              constConstants.TARGET_TYPE_SCENE,
                    //                              constConstants.TARGET_TYPE_DIARY,
                    //                              constConstants.TARGET_TYPE_PRODUCT,
                    //                              constConstants.TARGET_TYPE_COMMENT
                    //                          }));
                    //              
                    else if (method_name.equals("get" + TProduct._Fields.STATUS.getFieldName()))
                        should_be_among_field(product,
                            product_class_name,
                            product_method,
                            product_id,
                            Arrays.asList(new Integer[] {
                                constConstants.ENTITY_STATUS_NORM,
                                constConstants.ENTITY_STATUS_DEL,
                                constConstants.ENTITY_STATUS_PENDING,
                                constConstants.ENTITY_STATUS_REJECT }));
                    else if (method_name.equals("get" + TProduct._Fields.PLATFORM.getFieldName()))
                        should_be_among_field(product,
                            product_class_name,
                            product_method,
                            product_id,
                            Arrays.asList(new Integer[] {
                                constConstants.PLATFORM_ANDROID,
                                constConstants.PLATFORM_HTML5,
                                constConstants.PLATFORM_IOS,
                                constConstants.PLATFORM_PC }));

                    //              else if(method_name.equals("get"+TProduct._Fields.PUBLISH_SOURCE.getFieldName()))
                    //                  should_be_among_field(product,product_class_name,product_method,product_id,
                    //                          Arrays.asList(new Integer[]{
                    //                              constConstants.PUBLISH_DIARY_SOURCE_OWNER
                    //                          }));

                    else if (method_name.equals("get" + TProduct._Fields.CATEGORY_1.getFieldName())
                        || method_name.equals("get" + TProduct._Fields.CATEGORY_2.getFieldName()))
                    {
                        should_be_positive(product, TProduct.class.getName(), product_method, product_id);
                    }

                    else if (method_name.equals("get" + TProduct._Fields.CTIME.getFieldName())
                        || method_name.equals("get" + TProduct._Fields.UTIME.getFieldName()))
                        should_be_positive(product, product_class_name, product_method, product_id);
                }
                catch (Exception e)
                {
                    logger.error(e.getMessage());
                    continue;
                }

            }

            try
            {

                //RELATION VALIDATE
                //scene
                related_should_exist(product,
                    product_class_name,
                    product.getScene_id(),
                    TProduct._Fields.SCENE_ID.getFieldName(),
                    product_id,
                    Tables.Scene);

                //diary
                List<String> diary_list = product.getDiary_list();
                for (String diary_id : diary_list)
                {
                    related_should_exist(product, product_class_name, diary_id, TProduct._Fields.DIARY_LIST.getFieldName(), product_id, Tables.Diary);
                }

                //category
                //	category_1
                boolean category_1_among =
                    should_be_among_field(product,
                        product_class_name,
                        product_clazz.getMethod("getCategory_1"),
                        product_id,
                        new ArrayList(cate_map.keySet()));
                //	category_2
                if (category_1_among)
                    should_be_among_field(product,
                        product_class_name,
                        product_clazz.getMethod("getCategory_2"),
                        product_id,
                        new ArrayList(cate_map.get(product.getCategory_1()).keySet()));

                /* buyf_from 问题 要在FixDiaryBuyFrom中修正，并列出要核实的id  2014-12-5 by alpha.ren
                 * 由运营修复      
                 * */
                //buy_from
                //                if (!StringUtil.isEmpty(product.getBuy_from_1()))
                //                {
                //                    should_not_empty_field(product, product_class_name, product_clazz.getMethod("getBuy_from_1"), product_id);
                //
                //                    //buyfrom_1
                //                    should_be_among_field(product,
                //                        product_class_name,
                //                        product_clazz.getMethod("getBuy_from_1"),
                //                        product_id,
                //                        new ArrayList(buy_from_map.keySet()));
                //                    //	buyfrom_2
                //                    should_not_empty_field(product, product_class_name, product_clazz.getMethod("getBuy_from_2"), product_id);
                //                    should_be_among_field(product,
                //                        product_class_name,
                //                        product_clazz.getMethod("getBuy_from_2"),
                //                        product_id,
                //                        new ArrayList(buy_from_map.get(Integer.parseInt(product.getBuy_from_1())).keySet()));
                //                    should_not_empty_field(product, product_class_name, product_clazz.getMethod("getBuy_from_name"), product_id);
                //                    if ((product.getBuy_from_2().equals("1000") && (product.getBuy_from_2().equals("500")) && (!product.getBuy_from_name()
                //                        .equals(buy_from_map.get(Integer.parseInt(product.getBuy_from_1())).get(Integer.parseInt(product.getBuy_from_2()))))))
                //                    {
                //                        logger.error("buy from name:{} error on product with id:{}!", product.getBuy_from_name(), product_id);
                //                        throw new Exception();
                //                    }
                //                }

                //total_price
                if (product.getTotal_price() != product.getUnit_price() * product.getNum())
                    logger.error("total price error on product with id:{}!", product_id);

                if (product.getStatus() == constConstants.ENTITY_STATUS_REJECT)
                {
                    if (StringUtil.isEmpty(product.getRemark()))
                    {
                        logger.error("no remarks when reject on project of _id:{}", product_id);
                    }
                }
            }
            catch (Exception e)
            {
                if (!StringUtil.isEmpty(e.getMessage()))
                    logger.error(e.getMessage());
                continue;
            }

        }
    }

    /**
     * DIARY DATA VALIDATE		
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    static void validateDiary() throws Exception
    {
        Class<?> diary_clazz = TDiary.class;

        Progress prog = new Progress(Commons.mongo.count(Tables.Diary));
        for (String diary_id : Commons.mongo.ids(Tables.Diary))
        {
            logger.info("scanning diary:{}", diary_id);
            prog.increase(logger);
            TDiary diary = new TDiary();
            try
            {
                diary = validateDiary(diary_clazz, diary_id);
            }
            catch (Exception e)
            {
                //use unified TException
                logger.error("error of {} is {}", diary_id, e.getMessage());
                printDetails(diary, TDiary._Fields.CTIME.getFieldName());
            }
        }
    }

    static TDiary validateDiary(Class diary_clazz, String diary_id) throws Exception
    {
        String diary_class_name = TDiary.class.getName();
        DBObject diary_db = Commons.mongo.findById(Tables.Diary, diary_id);
        TDiary diary = ThriftUtil.dbObject2ThriftObject(diary_db, TDiary.class);

        if ((1 != diary.getStatus()) && (3 != diary.getStatus()))
            return diary;

        //FORMAT VALIDATE
        Method[] diary_methods = diary_clazz.getMethods();

        for (Method diary_method : diary_methods)
        {

            String method_name = diary_method.getName().toLowerCase();
            if (method_name.equals("get" + TDiary._Fields.SCENE_ID.getFieldName())
                || method_name.equals("get" + TDiary._Fields.CONTENT.getFieldName()))
                should_not_empty_field(diary, diary_class_name, diary_method, diary_id);

            else if (method_name.equals("get" + TDiary._Fields.STATUS.getFieldName()))
                should_be_among_field(diary,
                    diary_class_name,
                    diary_method,
                    diary_id,
                    Arrays.asList(new Integer[] {
                        constConstants.ENTITY_STATUS_NORM,
                        constConstants.ENTITY_STATUS_DEL,
                        constConstants.ENTITY_STATUS_PENDING,
                        constConstants.ENTITY_STATUS_DRAFT,
                        constConstants.ENTITY_STATUS_REJECT }));

            else if (method_name.equals("get" + TDiary._Fields.PLATFORM.getFieldName()))
                should_be_among_field(diary,
                    diary_class_name,
                    diary_method,
                    diary_id,
                    Arrays.asList(new Integer[] {
                        constConstants.PLATFORM_ANDROID,
                        constConstants.PLATFORM_HTML5,
                        constConstants.PLATFORM_IOS,
                        constConstants.PLATFORM_PC }));

            else if (method_name.equals("get" + TDiary._Fields.PUBLISH_SOURCE.getFieldName()))
                should_be_among_field(diary,
                    diary_class_name,
                    diary_method,
                    diary_id,
                    Arrays.asList(new Integer[] {
                        constConstants.PUBLISH_DIARY_SOURCE_OWNER,
                        constConstants.PUBLISH_DIARY_SOURCE_CRM_HT,
                        constConstants.PUBLISH_DIARY_SOURCE_JIANLI }));

            else if (method_name.equals("get" + TDiary._Fields.CTIME.getFieldName())
                || method_name.equals("get" + TDiary._Fields.UTIME.getFieldName())
                || method_name.equals("get" + TDiary._Fields.OTIME.getFieldName()))
                should_be_positive(diary, diary_class_name, diary_method, diary_id);
        }
        //RELATION VALIDATE
        //scene
        related_should_exist(diary, diary_class_name, diary.getScene_id(), TDiary._Fields.SCENE_ID.getFieldName(), diary_id, Tables.Scene);
        //progress
        //  progress_id
        should_be_among_field(diary, diary_class_name, diary_clazz.getMethod("getProgress_id"), diary_id, new ArrayList(progress_map.keySet()));
        //  tag_id
        should_be_among_field(diary,
            diary_class_name,
            diary_clazz.getMethod("getTag_id"),
            diary_id,
            new ArrayList(progress_map.get(diary.getProgress_id()).keySet()));

        //image num
        if (null == diary.getImages())
        {
            if (diary.getImage_num() != 0)
                logger.error("image num should be 0 on diary with id:{}!", diary_id);
        }
        else
        {
            if (diary.getImage_num() != diary.getImagesSize())
                logger.error("image num stat error on diary with id:{}!", diary_id);
        }

        //products
        List<String> products = diary.getProducts();
        if (null != products)
        {
            for (String product_id : products)
            {
                related_should_exist(diary, diary_class_name, product_id, TDiary._Fields.PRODUCTS.getFieldName(), diary_id, Tables.Product);
            }
        }

        //remark
        if (diary.getStatus() == constConstants.ENTITY_STATUS_REJECT)
        {
            if (StringUtil.isEmpty(diary.getRemark()))
            {
                logger.error("no remarks when reject on diary of _id:{}", diary_id);
            }
        }

        //owner_id
        if ((diary.getPublish_source() == constConstants.PUBLISH_DIARY_SOURCE_OWNER) && (diary.getOwner_id().isEmpty()))
        {
            logger.error("no owner_id on diary which is published by owner");
        }

        //stat 先不检查，这个问题还不很致命
        TDiaryStat stat = diary.getStat();

        return diary;
    }

    /**
     * SCENE DATA VALIDATE		
     * @throws Exception 
     */
    static void validateScene() throws Exception
    {
        Class<?> scene_clazz = TScene.class;
        String scene_class_name = TScene.class.getName();

        Progress prog = new Progress(Commons.mongo.count(Tables.Scene));
        for (String scene_id : Commons.mongo.ids(Tables.Scene))
        {
            prog.increase(logger);
            validateScene(scene_clazz, scene_id);
        }
    }

    static void validateScene(Class scene_clazz, String scene_id) throws Exception
    {
        String scene_class_name = scene_clazz.getName();

        DBObject scene_db = Commons.mongo.findById(Tables.Scene, scene_id);
        TScene scene = ThriftUtil.dbObject2ThriftObject(scene_db, TScene.class);
        //FORMAT VALIDATE
        Method[] scene_methods = scene_clazz.getMethods();
        for (Method scene_method : scene_methods)
        {
            String method_name = scene_method.getName().toLowerCase();
            if (method_name.equals("get" + TScene._Fields.STATUS.getFieldName()))
                should_be_among_field(scene,
                    scene_class_name,
                    scene_method,
                    scene_id,
                    Arrays.asList(new Integer[] {
                        constConstants.ENTITY_STATUS_NORM,
                        constConstants.ENTITY_STATUS_DEL,
                        constConstants.ENTITY_STATUS_PENDING,
                        constConstants.ENTITY_STATUS_REJECT }));

            else if (method_name.equals("get" + TScene._Fields.PLATFORM.getFieldName()))
                should_be_among_field(scene,
                    scene_class_name,
                    scene_method,
                    scene_id,
                    Arrays.asList(new Integer[] {
                        constConstants.PLATFORM_ANDROID,
                        constConstants.PLATFORM_HTML5,
                        constConstants.PLATFORM_IOS,
                        constConstants.PLATFORM_PC }));

            else if (method_name.equals("get" + TScene._Fields.PUBLISH_SOURCE.getFieldName()))
                should_be_among_field(scene,
                    scene_class_name,
                    scene_method,
                    scene_id,
                    Arrays.asList(new Integer[] {
                        constConstants.PUBLISH_SCENE_SOURCE_OWNER,
                        constConstants.PUBLISH_SCENE_SOURCE_CRM_ZB,
                        constConstants.PUBLISH_SCENE_SOURCE_ZXGS }));

            else if (method_name.equals("get" + TScene._Fields.CTIME.getFieldName())
                || method_name.equals("get" + TScene._Fields.UTIME.getFieldName()))
                should_be_positive(scene, scene_class_name, scene_method, scene_id);
        }

        //RELATION VALIDATE
        //progress
        //  progress_id
        TSceneStat stat = scene.getStat();
        if (null != stat && stat.getProg_stat_countsSize() > 0)
            should_be_among_field(scene, scene_class_name, scene_clazz.getMethod("getProgress_id"), scene_id, new ArrayList(progress_map.keySet()));

        //remark
        if (scene.getStatus() == constConstants.ENTITY_STATUS_REJECT)
        {
            if (StringUtil.isEmpty(scene.getRemark()))
            {
                logger.error("no remarks when reject on diary of _id:{}", scene_id);
            }
        }
    }

    public static void main(String arg[]) throws NoSuchMethodException, SecurityException
    {
        //分类map
        cate_map =
            getStConfMap(constConstants.CONF_CATE_TYPE_1, TProduct._Fields.CATEGORY_1, constConstants.CONF_CATE_TYPE_2, TProduct._Fields.CATEGORY_2);

        //购买地map
        buy_from_map =
            getStConfMap(constConstants.CONF_BUY_FROM_TYPE_1,
                TProduct._Fields.BUY_FROM_1,
                constConstants.CONF_BUY_FROM_TYPE_2,
                TProduct._Fields.BUY_FROM_2);

        //装修阶段
        progress_map =
            getStConfMap(constConstants.CONF_PROG_TYPE_1, TDiary._Fields.PROGRESS_ID, constConstants.CONF_PROG_TYPE_2, TDiary._Fields.TAG_ID);

        //装修类型map
        hometype_map = getStConfMap(constConstants.CONF_HOME_TYPE_1, TScene._Fields.CGATE, constConstants.CONF_HOME_TYPE_2, TScene._Fields.HOMETYPE);

        try
        {
            //        validateComment();
            //        validateProduct();
            //            validateDiary();
            //            validateScene(TScene.class, "45945");
            validateScene();
        }
        catch (TException e)
        {
            logger.error("catch");
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
        }
        finally
        {
            System.exit(0);
        }

    }
}
